package com.example.barcodescanner;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AudioActivity extends Activity {

	private static final String TAG = "MediaPlayerDemo";
    private MediaPlayer mMediaPlayer;
    private static final String MEDIA = "media";
    private static final int STREAM_AUDIO = 2;
    private static final int RESOURCES_AUDIO = 3;
    private String path;

    private static int TYPE = STREAM_AUDIO;  
    
    private int currentPosition = 0;

    private TextView tx;
    
	private Button mPlay;
	private Button mPause;
	private Button mReset;
	private Button mStop;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
 
        setContentView(R.layout.activity_audio);
        
        Bundle extras = getIntent().getExtras();
        TYPE = extras.getInt(MEDIA);               
        
		mPlay = (Button) findViewById(R.id.play);
		mPause = (Button) findViewById(R.id.pause);
		mReset = (Button) findViewById(R.id.rewind);
		mStop = (Button) findViewById(R.id.stop);
				
		 		 
		mPlay.setOnClickListener(playListener); 			
		mPause.setOnClickListener(pauseListener);
		mReset.setOnClickListener(rewindListener);
		mStop.setOnClickListener(stopListener);	
		
    }
    
    
    private OnClickListener playListener = new OnClickListener() {
		public void onClick(View view) {
			//check if the MediaPlayer has not been created yet, create it with first call to playLocalMedia
			if(mMediaPlayer != null) {
				//check to either start from a given position in the file or from the beginning
				if(currentPosition != 0) 
				{
					mMediaPlayer.start();
				}
				else 
				{
					playAudio();
				}
			}
			else 
			{
				playAudio();
			}
		}
	}; 
	
	
	
	private OnClickListener pauseListener = new OnClickListener() {
		public void onClick(View view) {
			if (mMediaPlayer != null) {
				mMediaPlayer.pause();
				currentPosition = mMediaPlayer.getCurrentPosition();
			}
		}
	};
	
	private OnClickListener rewindListener = new OnClickListener() {
		public void onClick(View view) {
			if (mMediaPlayer != null) {
				mMediaPlayer.seekTo(0);
			}
		}
	};
	
	
	private OnClickListener stopListener = new OnClickListener() {
		public void onClick(View view) {
			if (mMediaPlayer != null) {
				mMediaPlayer.stop();
				mMediaPlayer.reset();
			}
			currentPosition = 0;
			
		}
	};

    private void playAudio() {
        try {
            switch (TYPE) {

                case RESOURCES_AUDIO:
                	//This version of playing audio does it from the raw/res folder in the project folders.
                	//Typically used for adding audio resources to the application that play in the background (ex. games).
                	((TextView)this.findViewById(R.id.textView1)).setText("Playing Audio from Resources Folder...");
                    /**
                     * TODO: Upload a audio file to res/raw folder and provide
                     * its resid in MediaPlayer.create() method.
                     */
                	mMediaPlayer = new MediaPlayer();
                 	String uri = "android.resource://" + getPackageName() + "/" + R.raw.test_cbr;
                 	mMediaPlayer.setDataSource(this, Uri.parse(uri));
                 	mMediaPlayer.prepare();
                 	mMediaPlayer.start();
                    break;
                    
                case STREAM_AUDIO:
                	//This version is for streaming audio from an HTTP source. 
                	//This one has the potential for needing the async preparation if the streaming source takes a while.
                    //path = "http://sound17.mp3pk.com/indian/force/force01(www.songs.pk).mp3";
                	path =  getIntent().getExtras().getString("link");
                	System.out.println(path);
                	((TextView)this.findViewById(R.id.textView1)).setText("Playing Streaming Audio...\n" + path);

                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    break;
            }
            tx.setText("Playing audio...");

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
}
